﻿Public Class FormCheck
    Dim oper_u As String
    Public ty As String
    Dim objGt As New clsGt
    Dim objGtNgProcedure As New clsGtNgProcedure
    Dim objGtNgDetails As New clsGtNgDetails
    Private Sub FormCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Me.WindowState = FormWindowState.Maximized
        oper_u = GetIniFile("LOGIN", "user", "", g_szInitFileName)
        Dim startDate As String = Date.Now.AddDays(-1).ToShortDateString()
        StartDate_support_model.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        EndDate_support_model.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
    End Sub

    Public Sub setDgvData()
        If String.IsNullOrEmpty(StartDate_support_model.Text.Trim()) = False And String.IsNullOrEmpty(EndDate_support_model.Text.Trim()) = False Then
            Dim dtGt As DataTable = objGt.getGtList(StartDate_support_model.Text.Trim() + " 00:00:00.000",
                                                              EndDate_support_model.Text.Trim() + " 23:59:59.999", ty)
            dtGt.Columns.Add("oper_type_name")
            If dtGt.Rows.Count > 0 Then
                For Each row As DataRow In dtGt.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                    Else
                        row("oper_type_name") = "法兰板"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtGt
        Else
            MsgBox("请输入开始时间和结束时间")
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        Dim dtDetails As New DataTable
        If dgv.SelectedRows.Count > 0 Then
            dtDetails = objGtNgProcedure.getGtNgProcedureList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
            dtDetails.Columns.Add("procedure_no_name")
            If dtDetails.Rows.Count > 0 Then
                For Each row As DataRow In dtDetails.Rows
                    If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "校平"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "3" Then
                            row("procedure_no_name") = "六工位"
                        End If
                    End If
                    If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "三工位"
                        End If
                    End If

                Next
            End If
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtDetails
        setDgvNgData()
    End Sub

    Public Sub setDgvNgData()
        Dim dtNgDetails As New DataTable
        If dgv.SelectedRows.Count > 0 Then
            dtNgDetails = objGtNgDetails.getGtNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
            dtNgDetails.Columns.Add("procedure_no_name")
            dtNgDetails.Columns.Add("validity_name")
            If dtNgDetails.Rows.Count > 0 Then
                For Each row As DataRow In dtNgDetails.Rows
                    If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "校平"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "3" Then
                            row("procedure_no_name") = "六工位"
                        End If
                    End If
                    If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                        If row("procedure_no") = "0" Then
                            row("procedure_no_name") = "领料"
                        End If
                        If row("procedure_no") = "1" Then
                            row("procedure_no_name") = "喷砂清洗"
                        End If
                        If row("procedure_no") = "2" Then
                            row("procedure_no_name") = "三工位"
                        End If
                    End If
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
        End If
        dgvNg.AutoGenerateColumns = False
        dgvNg.DataSource = dtNgDetails
    End Sub

    Private Sub btnProcedure_Click(sender As Object, e As EventArgs) Handles btnProcedure.Click
        Dim formOper As New FormOper()
        formOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        formOper.oper_type = dgv.SelectedRows(0).Cells("oper_type").Value.ToString()
        formOper.ShowDialog()
    End Sub

    Private Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click
        If String.IsNullOrEmpty(oper_u) Then
            MsgBox("请切换用户进行登录！")
            Exit Sub
        End If
        If dgv.SelectedRows.Count > 0 Then
            If dgv.SelectedRows(0).Cells("Col_status").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行结束生产批次吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                    Dim rtn As Integer = objGt.updateStatus(sys_id, "2")
                    If rtn <> -2 Then
                        MsgBox("结束成功")
                        setDgvData()
                    Else
                        MsgBox("结束失败")
                    End If
                End If
            Else
                MsgBox("该记录已结束生产批次")
            End If
        End If
    End Sub

    Private Sub btnNgAdd_Click(sender As Object, e As EventArgs) Handles btnNgAdd.Click
        Dim formNGAdd As New FormNgAdd()
        formNGAdd.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        formNGAdd.oper_type = dgv.SelectedRows(0).Cells("oper_type").Value.ToString()
        formNGAdd.oper_type_name = dgv.SelectedRows(0).Cells("oper_type_name").Value.ToString()
        formNGAdd.type_value = dgv.SelectedRows(0).Cells("type_value").Value.ToString()
        formNGAdd.batch_no = dgv.SelectedRows(0).Cells("batch_no").Value.ToString()
        formNGAdd.picking_num = dgv.SelectedRows(0).Cells("picking_num").Value.ToString()
        formNGAdd.ng_num = dgv.SelectedRows(0).Cells("ng_num").Value.ToString()
        formNGAdd.ShowDialog(Me)
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If String.IsNullOrEmpty(oper_u) Then
            MsgBox("请切换用户进行登录！")
            Exit Sub
        End If
        If dgvNg.SelectedRows.Count > 0 Then
            If dgvNg.SelectedRows(0).Cells("validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNg.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNg.SelectedRows(0).Cells("Col_procedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNg.SelectedRows(0).Cells("Colb_gt_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNg.SelectedRows(0).Cells("Col_ng_num").Value.ToString()
                    Dim rtn As Integer = objGtNgDetails.updateValidity(sys_id, procedure_no, b_gt_sys_id, ng_num, oper_u)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        setDgvData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnFactoryOutsource_Click(sender As Object, e As EventArgs) Handles btnFactoryOutsource.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim assist_marker As String = dgv.SelectedRows(0).Cells("Col_assist_marker").Value.ToString()
            Dim formFactoryOutsourceSave As New FormFactoryOutsourceSave()
            formFactoryOutsourceSave.sys_id = sys_id
            formFactoryOutsourceSave.assist_marker = assist_marker
            formFactoryOutsourceSave.ShowDialog(Me)
        End If
    End Sub
End Class